package com.czxy.service;

import com.czxy.dao.OrderMapper;
import com.czxy.domain.Order;
import com.czxy.domain.OrderItem;
import com.czxy.utils.MyBatisUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.junit.Test;

import java.util.Date;
import java.util.List;

public class OrderService {



//    根据 用户的 uid 分页页码 和页大小,进行分页查询
//    查询该用户对应 所有的订单 ,返回 分页pageInfo对象

    public PageInfo findPageInfoByUid(String uid,int pageNum,int pageSize){
        //1.获取Mapper
        OrderMapper mapper = MyBatisUtils.getMapper(OrderMapper.class);
        //2.启动分页
        PageHelper.startPage(pageNum, pageSize);
        //3.查询
        List<Order> list = mapper.findOrdersByUid(uid);
        //4.封装结果 释放资源
        PageInfo<Order> pageInfo = new PageInfo<>(list);
        MyBatisUtils.close();
        //5.返回结果
        return pageInfo;
    }









    //存储订单
    public void addOrder(Order order){

        // 把 order的基本信息添加到 order表中
        OrderMapper mapper = MyBatisUtils.getMapper(OrderMapper.class);
        mapper.insert(order);
        MyBatisUtils.commitAndClose();

        // 把order中的 订单项集合 添加到订单项表中
        OrderItemService service = new OrderItemService();
        service.addItems(order.getItemList());

    }



    public void test01(){

        // 构建一个订单数据
        Order order = new Order();
        order.setOid("O0123");
        order.setOrdertime(new Date());
        order.setTotalPrice(100.0);
        order.setState(1);
        order.setAddress("北京");
        order.setName("张三");
        order.setTelephone("1111111");
        order.setUid("f55b7d3a352a4f0782c910b2c70f1ea4");

        OrderItem orderItem1 = new OrderItem("OI001", 10, 5.0, "1", "O0123",null);
        OrderItem orderItem2 = new OrderItem("OI002", 11, 8.0, "8", "O0123",null);

        order.getItemList().add(orderItem1);
        order.getItemList().add(orderItem2);
        // 测试 addOrder方法
        OrderService service = new OrderService();
        service.addOrder(order);

    }

}
